<style>
    #here{line-height: 30px;}
    .spanbutton{
        padding:5px 15px;background-color: #3F51B5;color: #FFF;cursor: pointer;
    }
    .shanchubutton{
        padding:3px 10px;background-color: #3F51B5;color: #FFF;cursor: pointer;
    }

</style>
<div>
    <form class="form-horizontal bv-form"  role="form"  id="addForm">
        <fieldset>
            <legend>基本信息</legend>
            <div class="form-group">
                <label class="col-sm-3 control-label">用户名</label>
                <div class="col-sm-8">
                    <select name="con_userid" class="SearchForm form-control"  style="width:200px;height:32px;" >
                        <option value="">选择用户</option>
                        {volist name='users' id='u'}
                        <option value="{$u['user_id']}">{$u['user_username']}</option>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">合约编号</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="con_number" value="">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">合约价格</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="con_money" value="">

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">合约份额</label>
                <div class="col-sm-8">
                    <input id="all" type="text" class="form-control" name="con_allmany" value="">
                </div>
            </div>
            <div class="form-group list_list">
                <label class="col-sm-3 control-label">&nbsp;</label>
                <div class="col-sm-8" id="here">
                    <!--<div>-->
                        <!--<span>uuuuuuuuuuuuuuuuuuuuuuuuuuuuu</span>-->
                        <!--<span data-comid="1" data-many="1" class="shanchubutton">删除</span>-->
                    <!--</div>-->
                    <!--<div>-->
                        <!--<span>uuuuuuuuuuuuuuuuuuuuuuuuuuuuu</span>-->
                        <!--<span data-comid="2" data-many="2" class="shanchubutton">删除</span>-->
                    <!--</div>-->
                    <!--<div>-->
                        <!--<span>uuuuuuuuuuuuuuuuuuuuuuuuuuuuu</span>-->
                        <!--<span data-comid="3" data-many="3" class="shanchubutton">删除</span>-->
                    <!--</div>-->
                </div>

            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <span style="color: red;">待分配：
                        <span style="color: red;" id="dai">0</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <span id="gofenpei" class="spanbutton">分配</span>
                    </span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">合约时间</label>
                <div class="col-sm-3">
                    <input type="text" readonly class="form-control" id="start_add" name="con_startdate" value="">
                </div>
                <div class="col-sm-3">
                    <input type="text" readonly class="form-control" id="end_add" name="con_enddate" value="">
                </div>
                <div class="col-sm-3">
                    <span id="spandays"></span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">维护费</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" name="con_fix" value="">
                    %
                </div>
            </div>
            <input type="hidden" id="con_days" name="con_zonedays" value="">
        </fieldset>
    </form>
</div>
<!--<button onclick="countMan();">text</button>-->
<script>
//    function countMan(){
//        $('#here').append('<div>pppppppppppppppppppp</div>');
//        $('#here').append('<div>pppppppppppppppppppp</div>');
//        $('#here').append('<div>pppppppppppppppppppp</div>');
//        $('#here').append('<div>pppppppppppppppppppp</div>');
//        $('#here').append('<div>pppppppppppppppppppp</div>');
//        return;
//        var all = parseInt($('#all).val());
//        var use = "";
//        $('.shanchubutton').each(function () {
//            var now = parseInt($(this).attr('data-many'));
//            use += "now:"+now;
//        });
//        alert(use);
//    }
    function countMany(zo){
        var all = parseInt($('#all').val());
        var use = 0;
        var usedata = "";
        $('.shanchubutton').each(function () {
            var now = parseInt($(this).attr('data-many'));
            use += now;
            usedata += $(this).attr('data-comid')+":"+$(this).attr('data-many')+',';
        });
        var have = all-use;
        if(zo == 0){return have;}
        if(zo == 1){return usedata;}
    }
    $('#all').keyup(function () {
        var all = parseInt($(this).val());
        if(all > 0 && all < 1000000){
            var have = countMany(0);
            $('#dai').text(have);
        }else{
            $('#dai').text(0);
        }
        return;
    });
$('.shanchubutton').click(function(){
    $(this).parent().remove();
    var all = parseInt($('#all').val());
    if(all > 0 && all < 1000000){
        var have = countMany(0);
        $('#dai').text(have);
    }else{
        $('#dai').text(0);
    }

});
    $('#gofenpei').click(function () {
        var dai = parseInt($('#dai').text());
        if(dai > 0){
            var usedata = countMany(1);
            $.ajax({
                url:'admin/Contract/add_showcom',
                data: 'mymax='+dai+'&usedata='+usedata,
                type: 'POST',
                dataType:'HTML',
                success:function(data){
                    var con = data;
                    $.TeachDialog({
                        title:'<h3>分配份额</h3>',
                        content:con,
                        showCloseButton: true,
                        showCloseButtonName: '关闭',
                        otherButtons: ['提交'],
                        otherButtonStyles: ['btn-primary'],
                        clickButton: function (sender, modal, index) {
                            var mymax = parseInt($('#mymax').val());
                            var have = 0;
                            var flag = true;
                            var flagnum = '';
                            $("input:checkbox[name='who']:checked").each(function(index,data){
                                var comid = data.value;
                                var many = parseInt($('#com_'+comid).val());
                                var m_big = parseInt($('#com_'+comid).attr('max'));
                                if(many > m_big){
                                    flag = false;
                                    flagnum = $('#com_'+comid).attr('data-num');
                                }
                                have += many;
                            });
                            if(flag === false){
                                alert('编号为：'+flagnum+'的机器超出可分配最大值');
                                return;
                            }
                            if(have > mymax){
                                alert('份额超出，没有这么多份额');
                                return;
                            }else{
                                $("input:checkbox[name='who']:checked").each(function(index,data){
                                    var comid = data.value;
                                    var many = parseInt($('#com_'+comid).val());
                                    var num = $(this).attr('data-num');
                                    var power = $(this).attr('data-power');
                                    var mypower = (power/10)*many;
                                    var mypower = Math.floor(mypower * 100) / 100;
                                    $('#here').append("<div>" +
                                        "<span>机器编号："+num+"份额："+many+"电费："+mypower+"元/天</span>&nbsp;&nbsp;&nbsp;" +
                                        "<span class='shanchubutton' data-comid='"+comid+"' data-many='"+many+"'>删除</span>" +
                                        "</div>");
                                });
                                var have22 = countMany(0);
                                $('#dai').text(have22);
                                $('.shanchubutton').click(function(){
                                    $(this).parent().remove();
                                    var all = parseInt($('#all').val());
                                    if(all > 0 && all < 1000000){
                                        var have = countMany(0);
                                        $('#dai').text(have);
                                    }else{
                                        $('#dai').text(0);
                                    }

                                });
                                modal.modal('hide');
                            }

                        }
                    })
                }
            })
        }else{
            alert('无可分配份额');
            return;
        }
    });

    //
function daysBetween(sDate1,sDate2){
//Date.parse() 解析一个日期时间字符串，并返回1970/1/1 午夜距离该日期时间的毫秒数
    var time1 = Date.parse(new Date(sDate1));
    var time2 = Date.parse(new Date(sDate2));
    var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24));
    return nDays;
};
$('#start_add').datepicker({
    format : "yyyy-mm-dd",
    todayBtn : "linked",
    autoclose : true,
    todayHighlight : true,
    clearBtn : true
});
$('#end_add').datepicker({
    format : "yyyy-mm-dd",
    todayBtn : "linked",
    autoclose : true,
    todayHighlight : true,
    clearBtn : true,
});
$('#start_add,#end_add').change(function () {
    var a = $('#start_add').val();
    var b = $('#end_add').val();
    if(a != "" && b != ""){
        var days = daysBetween(a,b);
        $('#spandays').text(days+'天');
        $('#con_days').val(days);
    }
});
</script>